package com.roger.tool.net;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.widget.Toast;

public class NetUtil {
	
	/**
	 * 检查是否联网
	 * @return
	 */
	public static boolean checkIfConnectNet(Context pContext){
		ConnectivityManager manager = (ConnectivityManager) pContext
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo info = manager.getActiveNetworkInfo();
		
		if (info != null && info.isConnected()) {
			return true;
		}
		return false;
	}
	
	/**
	 * 警告没有联网 要不要去设置面板设置网络
	 */
	public static void alertNoNet(final Context pContext){
		Builder builder = new AlertDialog.Builder(pContext);
		builder.setTitle("网络状态");
		builder.setMessage("没有可用网络,是否进入设置面板");
		builder.setPositiveButton("是",
				new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						pContext.startActivity(new Intent(android.provider.Settings.ACTION_WIRELESS_SETTINGS));
					}
				});
		builder.setNegativeButton("否",
				new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						Toast.makeText(pContext, "联网失败",
								Toast.LENGTH_SHORT).show();
					}
				});
		builder.create().show();
	}

}
